<template>
	<view class="content">
		<uv-navbar autoBack placeholder bgColor="transparent" :fixed="false" title="分享邀请"></uv-navbar>
		<view style="width: 750rpx; 
		height: 1000rpx; 
		margin-top: 30rpx;
		padding: 20rpx 30rpx;
		box-sizing: border-box;">
			<PosterSwiper v-model="activeIndex" :images="banners" qrCode="" :isShowPosterNum="false" />
		</view>
		<view class="bottom-bt-box">
			<tui-button type="danger" width="600rpx" height="70rpx" size="30" @click="saveImg">保存分享海报</tui-button>
		</view>
		<uv-popup ref="uvPopupShare" round="10" :closeOnClickOverlay="false" :closeable="true" closeIconPos="top-right">
			<view class="image-box">
				<image :src="activeIndex==0?shareImage1:shareImage2"></image>
			</view>
		</uv-popup>
    </view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		onShareAppMessage,
		onShareTimeline
	} from '@dcloudio/uni-app'
	import {
		getShareInfo
	} from '@/api/other.js'
	import {
		navigate,
		upgradeToHttps
	} from '@/util/tools.js'
	import {
		storeToRefs
	} from 'pinia'
	import {
		useUserStore
	} from '@/store'
	import PosterSwiper from "@/components/PosterSwiper.vue";
	const activeIndex = ref(0);
    const user = useUserStore()
	const {
		token,
		userInfo
	} = storeToRefs(user)
	const uvPopupShare = ref(null)
	const shareImage1 = ref('');
	const shareImage2 = ref('');
	const banners = ref([])
	const handGetShareInfo = async () => {
		const res = await getShareInfo()
		if (res.code === 200) {
			let data = res.result
			let temp = {}
			let temp2 = {}
			shareImage1.value = upgradeToHttps(data.loop_img1)
			temp.src = upgradeToHttps(data.loop_img1)
			temp.title = '成为企业商家'
			temp.titleStyle = {
				color: '#f60',
				fontSize: '48rpx',
				fontWeight: 600,
				fontStyle: 'italic'
			}
			shareImage2.value = upgradeToHttps(data.loop_img2)
			temp2.src = upgradeToHttps(data.loop_img2)
			temp2.title = '成为会员'
			temp2.titleStyle = {
				color: 'red',
				fontSize: '48rpx',
				fontStyle: 'italic',
				fontWeight: 600,
			}
			banners.value.push(temp2,temp)
		}

	}
	
	const saveImg = () => {
		let selfImg = ''
		let type = 0
		if (activeIndex.value == 0) {
			selfImg = shareImage2.value
			type = 1
		} else {
			selfImg = shareImage1.value
			type = 2
		}
		// 先下载图片到本地
		uni.downloadFile({
			url: selfImg,
			success: (res) => {
				if (res.statusCode === 200) {
					// 保存到相册
					uni.saveImageToPhotosAlbum({
						filePath: res.tempFilePath,
						success: () => {
							uni.showToast({
								title: '已将分享海报保存到相册中',
								icon: 'success'
							})
						},
						fail: (err) => {
							console.log('海报保存失败', err)
							uni.showToast({
								title: '海报保存失败',
								icon: 'none'
							})
						}
					})
				}
			}
		})
	}
		
		
		
	const shareAction = () => {
		uvPopupShare.value?.open('center')
	}

	onShareAppMessage(() => {
		let selfImg = ''
		let type = 0
		if (activeIndex.value == 0) {
			selfImg = shareImage1.value
			type = 1
		} else {
			selfImg = shareImage2.value
			type = 2
		}
		let pathUrl='/pages/auth/index?source=' + userInfo.value.id + '&type=' + type
		console.log('--onSharepathUrl--',pathUrl)
		return {
			title: '欢迎加入邻shi通',
			path: pathUrl,
			imageUrl: selfImg
		}
	})

	onShareTimeline(() => {
		let selfImg = ''
		let type = 0
		if (activeIndex.value == 0) {
			selfImg = shareImage1.value
			type = 1
		} else {
			selfImg = shareImage2.value
			type = 2
		}
		return {
			title: '欢迎加入邻shi通',
			query: 'source=' + userInfo.value.id + '&type=' + type,
		}
	})



	const init = async () => {
		await handGetShareInfo()
	}



	init()
	
</script>

<style>
	page {
		background: linear-gradient(to bottom, #f7a6ac, #f6dfdf);
	}
</style>
<style lang="scss" scoped>
	.content {}

	.bottom-bt-box {
		padding: 30upx;
		box-sizing: border-box;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.image-box {
		width: 600upx;
		height: 900upx;

	}

	.image-box image {
		width: 100%;
		height: 100%;
	}

	::v-deep .uv-safe-area-inset-bottom.data-v-560f16b2 {
		padding-bottom: 0 !important;
	}
</style>